package com.qf.java2007.web.controller;

import com.qf.java2007.pojo.BookInfo;
import com.qf.java2007.pojo.ReaderInfo;
import com.qf.java2007.service.BookInfoService;
import com.qf.java2007.service.ReaderCardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author AzureSky_X
 * @Date 2021/3/9 16:48
 * @Version 1.0
 */
@Controller
@RequestMapping("/reader")
public class ReaderCardController {
    @Autowired
    private BookInfoService bookInfoService;

    @Autowired
    private ReaderCardService readerCardService;

    /**
     * 显示头部导航栏
     *
     * @return
     */
    @GetMapping("/header")
    public String readerHeader() {
        return "reader_header";
    }

    /**
     * 到借阅人主界面
     *
     * @return
     */
    @GetMapping("/main")
    public String readerMain() {
        return "reader_main";
    }

    /**
     * 查询借阅人个人信息
     *
     * @return
     */
    @GetMapping("/info")
    public ModelAndView readerInfo(Long readerId) {
        ReaderInfo readerInfo = readerCardService.findReaderCardById(readerId);
        ModelAndView mav = new ModelAndView("reader_info");
        mav.addObject("readerInfo", readerInfo);

        return mav;
    }

    /**
     * 到达修改界面，并显示信息
     *
     * @param readerId
     * @return
     */
    @GetMapping("/infoEditUI")
    public ModelAndView readerInfoEdit(Long readerId) {
        ReaderInfo readerInfo = readerCardService.findReaderCardById(readerId);
        ModelAndView mav = new ModelAndView("reader_info_edit");
        mav.addObject("readerInfo", readerInfo);
        return mav;
    }

    /**
     * 修改借阅人信息并跳转到信息展示页面
     *
     * @param readerInfo
     * @return
     */
    @PostMapping("/updateReader")
    public ModelAndView update(ReaderInfo readerInfo) {
        readerCardService.update(readerInfo);

        ModelAndView mav = new ModelAndView("reader_info");
        mav.addObject("succ", "修改成功！！！");
        return mav;
    }

    /**
     * 到更新密码界面
     *
     * @param readerId
     * @return
     */
    @GetMapping("/repasswdUI")
    public ModelAndView readerRepasswd(Long readerId) {
        //ReaderInfo readerInfo= readerCardService.findReaderCardById(readerId);
        ModelAndView mav = new ModelAndView("reader_repasswd");

        //mav.addObject("readerInfo",readerInfo);
        return mav;
    }

    /**
     * 更新密码
     *
     * @param
     * @return
     */
    @PostMapping("/updatePwd")
    public ModelAndView updatePwd(String reNewPasswd, String readerId) {
        readerCardService.updatePwd(reNewPasswd, readerId);

        ModelAndView mav = new ModelAndView("reader_repasswd");

        mav.addObject("succ", "修改成功！！！");
        return mav;
    }

    /**
     * 列出所有书籍信息
     *
     * @return
     */
    @GetMapping("/books")
    public ModelAndView list() {

        List<BookInfo> list = bookInfoService.findAll();
        ModelAndView mav = new ModelAndView("reader_books");
        mav.addObject("bookInfos", list);
        return mav;
    }

    /**
     * 查看书籍详情
     *
     * @param bookId
     * @return
     */
    @GetMapping("/book/detail")
    public ModelAndView detail(Long bookId) {

        BookInfo bookInfo = bookInfoService.findByIdWithClass(bookId);
        ModelAndView mav = new ModelAndView("reader_book_detail");
        mav.addObject("bookInfo", bookInfo);
        return mav;
    }

    /**
     * 根据用户输入的关键字模糊查询出书籍信息
     *
     * @param searchWord
     * @return
     */
    @PostMapping("/search")
    public ModelAndView search(String searchWord) {
        List<BookInfo> list = bookInfoService.findByNameOrAuthor(searchWord);
        ModelAndView mav = new ModelAndView("reader_books");
        mav.addObject("bookInfos", list);
        mav.addObject("searchWord", searchWord);
        return mav;
    }


}
